<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Marketing as MarketingModel;

class MarketingService
{
    /**
     * 活动列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getMarketingList($where, $page, $pagesize)
    {
        $list = MarketingModel::getMarketingList($where, $page, $pagesize);
        foreach ($list as $k => $v) {
            $list[$k]['ids'] = $v['id'] . '<br><span class="color3">' . $v['budget'] . '</span>';
            // 二维码
            $qrcode = '<div><img class="otherimages" title="点我查看原图" onclick="lookimg(this.src)" src="' . config('url.url') . $v['qrcode'] . '" \' + \'alt=""></div>';
            // 地区
            if (strrpos($v['city_name'], '市')) {
                $v['city_name'] = substr_replace($v['city_name'], '', strrpos($v['city_name'], '市'));
            }
            // 活动主题数据
            $list[$k]['title_data'] = '<div class="d-fix1 mar-lefts">' . $qrcode . '<p class="datas">' . $v['title'] . '/<span class="color3">' . $v['city_name'] . '</span></p></div>';

            // 类型
            $type_name = $v['type'] == 1 ? '线上' : '线下';
            $list[$k]['type_data'] = $type_name . '<br><span class="color3">' . $v['manager_name'] . '</span>';

            // 活动时间
            $list[$k]['time_data'] = date('Y-m-d H:i', $v['start_time']) . '<br><span class="color3">' . date('Y-m-d H:i', $v['end_time']) . '</span>';

            // 新用户
            $newUsersNums = MarketingUsersService::countMarketingUsers(['m.mid' => $v['id']]);
            // 转换用户
            $map = ['m.is_convert' => 1, 'm.mid' => $v['id']];
            $changeUsersNums = MarketingUsersService::countMarketingUsers($map);
            $list[$k]['users_data'] = '<span style="cursor: pointer" title="新用户列表">' . $newUsersNums . '</span><br><span class="color3" style="cursor: pointer" title="转换人数列表">' . $changeUsersNums . '</span>';

            // 消费金额
            $money = MarketingUsersService::sumMarketingUsersPrice($map);
            // 获取该活动经办人的提成点
            $commission_ratio = OfficialStaffService::findOfficialStaffValue(['id' => $v['manager']], 'commission_ratio');
            // 佣金计算
            $commission = sprintf("%.2f", $money * $commission_ratio);
            $list[$k]['price_data'] = $money . '<br><span class="color3">' . $commission . '</span>';
        }
        return $list;
    }

    /**
     * 活动列表统计
     * @param $where
     * @return array
     */
    public static function countMarketingList($where)
    {
        return MarketingModel::countMarketingList($where);
    }

    /**
     * 单活动
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findMarketing($where)
    {
        return MarketingModel::findMarketing($where);
    }

    /**
     * 创建活动
     * @param $data
     * @return MarketingModel|\think\Model
     */
    public static function createMarketing($data)
    {
        return MarketingModel::createMarketing($data);
    }

    /**
     * 更新活动
     * @param $data
     * @param $where
     * @return MarketingModel
     */
    public static function updateMarketing($data, $where)
    {
        return MarketingModel::updateMarketing($data, $where);
    }
}